/*
 * @author: huangww
 * @Date: 2021-02-25 16:51:26
 * @LastEditTime: 2021-03-01 14:36:57
 * @FilePath: \fmgr\src\core\mgr\DBMgr.ts
 */
namespace core {
  export class DBMgr {
    private static instance: DBMgr;
    private pool = {};
    //加载龙骨资源
    public loadedRes = [];
    public releaseData = [];
    //龙骨工厂
    private factory: dragonBones.EgretFactory;
    public constructor() {
      this.factory = dragonBones.EgretFactory.factory;
      this.pool = {};
    }

    create(name: string): dragonBones.Armature {
      let armature = this.factory.buildArmature(name);
      return armature;
    }

    /**
     * 动态加载龙骨资源组
     * @param name 资源组名字需与龙骨名字相同
       @param count 龙骨资源拆分数量
       @param call 加载完回调
       @param retain 是否需要释放资源 默认保留
     */
    public loadArmature(name: string, count: number, call: Function, retain: boolean = true) {
      let armature = this.factory.buildArmature(name);
      if (!armature) {
        ResLoadMgr.getInstance().load(name, this, (event: RES.ResourceEvent) => {
          this.loadDragonBones(name, count, retain);
          if (!armature) {
            setTimeout(() => {
              armature = this.factory.buildArmature(name);
              call(armature);
            }, 100);
          } else {
            call(armature);
          }

        }, LOADTYPE.NORMAL);
        ResLoadMgr.getInstance().retainGroup(name, retain);
      } else {
        call(armature);
      }
    }

    parse(parseList: Array<DbParse>) {
      for (let i of parseList) {
        this.loadDragonBones(i.name, i.count, i.retain);
      }
    }

    /**
     * 加载龙骨资源组
     * @param name 资源名称
     * @param texNo 素材数量
     * @param 分组
     */
    private loadDragonBones(name: string, texNo: number, retain = true) {
      for (let i in this.loadedRes) {
        if (name == this.loadedRes[i]) {
          return;
        }
      }
      if (!retain && this.releaseData.indexOf(name) == -1) {
        this.releaseData.push(name);
      }
      let res = RES.getRes(name + "_ske_json");
      if (!res) {
        // console.warn(`解析龙骨失败，未找到资源 ${name}`);
        return;
      }
      this.factory.parseDragonBonesData(res);
      if (texNo > 1) {
        for (let i = 0; i < texNo; i++) {
          this.factory.parseTextureAtlasData(RES.getRes(name + "_tex_" + i + "_json"), RES.getRes(name + "_tex_" + i + "_png"));
        }
      } else {
        this.factory.parseTextureAtlasData(RES.getRes(name + "_tex_json"), RES.getRes(name + "_tex_png"));
      }
      this.loadedRes.push(name);
    }

    //释放龙骨资源
    public release() {
      console.log("releaseDragonData", this.releaseData)
      for (let i of this.releaseData) {
        let index = this.loadedRes.indexOf(i);
        if (index != -1) {
          this.factory.removeDragonBonesData(i);
          this.factory.removeTextureAtlasData(i);
          this.loadedRes.splice(index, 1);
        }
      }
      this.releaseData = [];
      this.releaseData.length = 0;

    }

    /**
     * @param v 骨架armature
     * @param viewName 标记骨架缓存所属
     */
    add(v, viewName: DBVIEW = null) {
      this.factory.clock.add(v);
      if (viewName) {
        if (!this.pool[viewName]) {
          this.pool[viewName] = [];
        }
        this.pool[viewName].push(v);
      }
    }

    remove(v) {
      this.factory.clock.remove(v);
    }

    /**从龙骨世界中移除一组骨骼 */
    dispose(viewName: DBVIEW = null) {
      if (this.pool[viewName]) {
        for (let i of this.pool[viewName]) {
          this.remove(i);
        }
      }
    }

    advanceTime(dt) {
      this.factory.clock.advanceTime(dt);
    }






    static getInstance() {
      if (!this.instance) {
        this.instance = new DBMgr();
      }
      return this.instance;
    }
  }
}